package gsw.tool.http.chain;

import android.util.Log;

import java.io.IOException;

import gsw.tool.http.Response;
import gsw.tool.http.call.Call;

/**
 * 请求失败时，重新请求
 */
public class InterceptorRetry implements Interceptor {

    /**
     * 重新请求
     */
    @Override
    public Response intercept(InterceptorChain chain) throws IOException {
        Log.e("interceprot", "重试拦截器....");
        Call call = chain.call;
        IOException exception = null;
        for (int i = 0; i < chain.call.client().retrys(); i++) {
            if (call.isCanceled()) {
                throw new IOException("Canceled");
            }
            try {
                //调用下一个拦截器
                Response response = chain.proceed();
                return response;
            } catch (IOException e) {
                exception = e;
            }
        }
        throw exception;
    }
}
